package org.grow.secure.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.grow.secure.dao.UserInfoDao;
import org.grow.secure.entity.UserInfo;
import org.grow.secure.entity.UserLoginDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Service;


import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @Author: xwg
 * @CreateDate: 21-7-15
 */
@Slf4j
@Service
public class AuthHandler implements AuthenticationSuccessHandler,
        AuthenticationFailureHandler {

    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest,
                                        HttpServletResponse httpServletResponse,
                                        Authentication authentication)
            throws IOException, ServletException {
//        Cookie cookie = new Cookie("businessCookie", "111111111");
//        httpServletResponse.addCookie(cookie);
//        System.out.println("principle: jdk 对userInfo的称呼 "+
//                authentication.getPrincipal().getClass());
        UserInfo principal = (UserInfo) authentication.getPrincipal();
//        System.out.println(principal);
        httpServletResponse.setStatus(200);
        UserLoginDto userLoginDto = new UserLoginDto();
//        userLoginDto.setNickname(principal.getNickname());
//        userLoginDto.setUserId(principal.getUserId());
//        userLoginDto.setUsername(principal.getUsername());
        BeanUtils.copyProperties(principal,userLoginDto);
//        Map<String,Object>  dto
//        开发坐火箭 维护火葬场
        String s = objectMapper.writeValueAsString(userLoginDto);

//        CharacterEncodingFilter
//                HttpEncodingAutoConfiguration;
//        String s = objectMapper.writeValueAsString(principal);
        httpServletResponse.getWriter().println(s);

        principal.setLastLoginTime(LocalDateTime.now());
        userInfoDao.save(principal);


        log.info("用户"+principal.getUsername()+"登录系统");

    }

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest,
                                        HttpServletResponse httpServletResponse,
                                        AuthenticationException e) throws IOException, ServletException {
//        todo: 100
        System.out.println(e);
        System.out.println("具体异常类型 "+e.getClass());
        if(e instanceof BadCredentialsException){
            httpServletResponse.setStatus(960);
            httpServletResponse.getWriter().println(e.getMessage());
        }
            //DisabledException
        else if (e instanceof DisabledException){
            httpServletResponse.setStatus(961);
            httpServletResponse.getWriter().println(e.getMessage());
        }

        else {
            httpServletResponse.setStatus(930);
            httpServletResponse.getWriter().println(e.getMessage());

        }
    }


}
